﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Specialized;
using ASBCC.WebCache;

namespace ASBCC.Database.Service
{
    public class MailService
    {
        #region Method
        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_coTagstitle">替换标题标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        /// <param name="_bodyFormat">bodyFormat</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags, NameValueCollection _coTagstitle,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName, string _bodyFormat)
        {
            //Edit By 沈冯杰 20090604
            string[] to = _arrMailAdds;
            string from = "";//Vela3Configuration.MailSender;

            DataSet ds = GetMailTemplateByIdDB(_mailTemplateId);

            if (ds == null || ds.Tables[0].Rows.Count == 0)
                return false;

            try
            {
                string subject = ds.Tables[0].Rows[0]["MailSubject"].ToString();
                string body = ds.Tables[0].Rows[0]["MailContent"].ToString();

                //替换邮件标题里的标签
                for (int i = 0; i < _coTagstitle.Count; i++)
                {
                    subject = subject.Replace(_coTagstitle.AllKeys[i], _coTagstitle[i]);
                }

                //替换邮件模板里的标签
                for (int i = 0; i < _coTags.Count; i++)
                {
                    body = body.Replace(_coTags.AllKeys[i], _coTags[i]);
                }

                //body = (body.Replace(" ", "&nbsp;")).Replace("\n", "<br>");
                //body += "<p><p><p>" + sign;
                MailSenderBasic(to, from, subject, body, ds.Tables[0].Rows[0]["TemplateCode"].ToString(), _cc, _bcc, _mailFrom, _displayName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        /// <param name="_bodyFormat">bodyFormat</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName, string _bodyFormat)
        {
            NameValueCollection collection = new NameValueCollection();
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, collection, _cc, _bcc, _mailFrom, _displayName, _bodyFormat);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_coTagstitle">替换标题标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags, NameValueCollection _coTagstitle,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName)
        {
            string _bodyFormat = "HTML";
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, _coTagstitle, _cc, _bcc, _mailFrom, _displayName, _bodyFormat);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName)
        {
            NameValueCollection collection = new NameValueCollection();
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, collection, _cc, _bcc, _mailFrom, _displayName);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_coTagstitle">替换标题标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags, NameValueCollection _coTagstitle,
            string[] _cc)
        {
            string[] _bcc = new string[] { };
            string _mailFrom = string.Empty;
            string _displayName = string.Empty;
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, _coTagstitle, _cc, _bcc, _mailFrom, _displayName);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_cc">cc</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags,
            string[] _cc)
        {
            NameValueCollection collection = new NameValueCollection();
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, collection, _cc);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <param name="_coTagstitle">替换标题标签的数组</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags, NameValueCollection _coTagstitle)
        {
            string[] _cc = new string[] { };
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, _coTagstitle, _cc);
        }

        /// <summary>
        /// 使用邮件模板发送邮件
        /// </summary>
        /// <param name="_arrMailAdds">收件人地址(字符串数组)</param>
        /// <param name="_mailTemplateId">模板ID</param>
        /// <param name="_coTags">替换模板标签的数组</param>
        /// <returns>是否发送成功</returns>
        public static bool MailSender(string[] _arrMailAdds, int _mailTemplateId, NameValueCollection _coTags)
        {
            NameValueCollection collection = new NameValueCollection();
            return MailSender(_arrMailAdds, _mailTemplateId, _coTags, collection);
        }
        #endregion

        #region basicMethod

        #region remark
        ///// <summary>
        ///// 邮件发送:基础方法
        ///// </summary>
        ///// <param name="_To">收件人地址(字符串数组)</param>
        ///// <param name="_From">发件人地址(默认为web.config中配置的发件人)系统地址</param>
        ///// <param name="_Subject">主题</param>
        ///// <param name="_Body">邮件内容</param>
        ///// <param name="_Profile_Name">TemplateCode</param>
        ///// <param name="_cc">cc</param>
        //private static void MailSenderBasic(string[] _To, string _From, string _Subject, string _Body, string _Profile_Name,
        //    string _cc)
        //{
        //    string _bcc = string.Empty;
        //    MailSenderBasic(_To, _From, _Subject, _Body, _Profile_Name, _cc, _bcc);
        //}


        ///// <summary>
        ///// 邮件发送:基础方法
        ///// </summary>
        ///// <param name="_To">收件人地址(字符串数组)</param>
        ///// <param name="_From">发件人地址(默认为web.config中配置的发件人)系统地址</param>
        ///// <param name="_Subject">主题</param>
        ///// <param name="_Body">邮件内容</param>
        ///// <param name="_Profile_Name">TemplateCode</param>
        ///// <param name="_cc">cc</param>
        ///// <param name="_bcc">bcc</param>
        //private static void MailSenderBasic(string[] _To, string _From, string _Subject, string _Body, string _Profile_Name,
        //    string _cc, string _bcc)
        //{
        //    string _mailFrom = string.Empty;
        //    string _displayName = string.Empty;
        //    MailSenderBasic(_To, _From, _Subject, _Body, _Profile_Name, _cc, _bcc, _mailFrom, _displayName);
        //}
        #endregion

        /// <summary>
        /// 邮件发送:基础方法
        /// </summary>
        /// <param name="_To">收件人地址(字符串数组)</param>
        /// <param name="_From">发件人地址(默认为web.config中配置的发件人)系统地址</param>
        /// <param name="_Subject">主题</param>
        /// <param name="_Body">邮件内容</param>
        /// <param name="_Profile_Name">TemplateCode</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        private static void MailSenderBasic(string[] _To, string _From, string _Subject, string _Body, string _Profile_Name,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName)
        {
            string _bodyFormat = "HTML";
            MailSenderBasic(_To, _From, _Subject, _Body, _Profile_Name, _cc, _bcc, _mailFrom, _displayName, _bodyFormat);
        }


        /// <summary>
        /// 邮件发送:基础方法
        /// </summary>
        /// <param name="_To">收件人地址(字符串数组)</param>
        /// <param name="_From">发件人地址(默认为web.config中配置的发件人)系统地址</param>
        /// <param name="_Subject">主题</param>
        /// <param name="_Body">邮件内容</param>
        /// <param name="_Profile_Name">TemplateCode</param>
        /// <param name="_cc">cc</param>
        /// <param name="_bcc">bcc</param>
        /// <param name="_mailFrom">发件人地址(实际发件人)</param>
        /// <param name="_displayName">displayName</param>
        /// <param name="_bodyFormat">bodyFormat</param>
        private static void MailSenderBasic(string[] _To, string _From, string _Subject, string _Body, string _Profile_Name,
            string[] _cc, string[] _bcc, string _mailFrom, string _displayName, string _bodyFormat)
        {
            ProcedureManager.usp3_IMP_SEND_MAIL(String.Join(";", _bcc), _bodyFormat, _Body, String.Join(";", _cc), _displayName, _mailFrom, _Profile_Name, String.Join(";", _To), _Subject);
        }
        #endregion

        #region webCache
        private static DataSet GetMailTemplateByIdDB(int _Id)
        {
            IWebCache webCache = GetWebCacheRealtime();
            string key = "MailTemplateID" + _Id.ToString();
            object value = webCache.Get(key);
            DataSet ds = new DataSet();
            if (value == null)
            {
                try
                {
                    ds = ProcedureManager.usp3_SelectMailTemplateByID(_Id);
                }
                catch
                {
                    ds = null;
                }
                webCache.Add(key, ds);
            }
            else
            {
                ds = value as DataSet;
            }
            return ds;
        }

        private static IWebCache GetWebCacheRealtime()
        {
            return new SqlDependencyWebCache(DatabaseManager.ConnectionString);
        }
    }
        #endregion
}
